package com.li.recycleradapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

/**
 * Created by Li on 2017/3/15.
 * RecyclerView  Holder
 */

public class ViewHolder extends RecyclerView.ViewHolder {
    private Context mContext;
    private SparseArray<View> mViews;
    private View mConvertView;
    public ViewHolder(Context context, View itemView) {
        super(itemView);
        mContext=context;
        mViews=new SparseArray<>();
        mConvertView=itemView;
    }

    private <T extends View> T getView(int viewId){

        View view = mViews.get(viewId);
        if (view == null) {
            view=mConvertView.findViewById(viewId);
            mViews.put(viewId,view);
        }
        return (T) view;
    }

    public ViewHolder setText(int viewId,String text){
        TextView textView= getView(viewId);
        textView.setText(text);
        return this;
    }

    public ViewHolder setImage(int viewId,String imgUrl){
        ImageView imageView= getView(viewId);
        Glide.with(mContext).load(imgUrl).into(imageView);
        return this;
    }
    public ViewHolder setNoCacheImage(int viewId,String imgUrl){
        ImageView imageView= getView(viewId);
        Glide.with(mContext)
                .load(imgUrl)
                .into(imageView);
        return this;
    }

}
